package util

import (
	"strings"
)

// 构建url地址
func BuildUrl(path string, vars ...map[string]any) string {
	url := path
	if !strings.HasPrefix(url, "http") && !strings.HasPrefix(url, "/") {
		url = "/" + url
	}
	if len(vars) > 0 && vars[0] != nil {
		queryStrs := make([]string, 0, len(vars[0]))
		for k, v := range vars[0] {
			vStr, vStrErr := ToString(v)
			if vStrErr != nil {
				continue
			}
			queryStrs = append(queryStrs, k+"="+vStr)
		}
		if len(queryStrs) > 0 {
			url += "?" + strings.Join(queryStrs, "&")
		}
	}
	return url
}
